﻿using Modbus.Messages;

namespace Modbus
{
	partial class ModbusClient
	{
		public CommEventCounter GetCommEventCounter(byte slaveAddress)
		{
			//
			// Input arguments validation.
			//

			Ensure.ArgumentNotEqualTo(SR.Argument_slaveAddress, slaveAddress, BroadcastAddress);

			//
			// Internal state validation.
			//

			this.ThrowIfDisposed();

			//
			// Request creation.
			//

			ModbusMessage request = new ModbusMessage();
			request.FunctionCode = FunctionCodes.GetCommEventCounter;

			//
			// Perform transaction.
			//

			var response = this.transport.Request<GetCommEventCounterResponse>(slaveAddress, request);

			//
			// Prepare return value.
			//

			CommEventCounter counter = new CommEventCounter(response.Status, response.EventCount);

			return counter;
		}
	}
}
